package com.ewanghuiju.app.ui.pintuan.adapter;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.component.ImageLoader;
import com.ewanghuiju.app.model.bean.response.PingjiaListResponBean;
import com.ewanghuiju.app.ui.pintuan.adapter.pictureselector.GlideEngine;
import com.ewanghuiju.app.util.GlideSimpleLoader;
import com.ewanghuiju.app.util.StringUtil;
import com.ewanghuiju.app.widget.FixImageview;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.makeramen.roundedimageview.RoundedImageView;
import com.willy.ratingbar.ScaleRatingBar;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class PintuanPingjiaAdapter extends BaseQuickAdapter<PingjiaListResponBean, BaseViewHolder> {

    public PintuanPingjiaAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, PingjiaListResponBean item) {
        String nickName = StringUtil.getLengthTextByStr(item.getNickname(), 7);
        helper.setText(R.id.tv_nickname, nickName);
        helper.setText(R.id.tv_date, item.getCreate_time());
        helper.setText(R.id.tv_content, item.getContent());
        helper.setText(R.id.tv_guige, item.getName_value());
        RoundedImageView iv_head = helper.getView(R.id.riv_head);
        ImageLoader.load(mContext, item.getUser_image(), iv_head);
        ScaleRatingBar scaleRatingBar = helper.getView(R.id.simpleRatingBar);
        scaleRatingBar.setRating(item.getStars());
        LinearLayout layout_one_pic = helper.getView(R.id.layout_one_pic);
        RecyclerView recyclerView = helper.getView(R.id.img_recyclerView);
        if (item.getImage() != null) {
            if (item.getImage().size() == 1) {
                layout_one_pic.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
                FixImageview riv_img = helper.getView(R.id.riv_img);
                ImageLoader.load(mContext, item.getImage().get(0), riv_img);
                riv_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        if (mGlideSimpleLoader != null) {
//                            mGlideSimpleLoader.showUrl(item.getImage(), 0);
//                        }
                        if (item.getImage() != null && item.getImage().size() > 0) {
                            List<LocalMedia> selectList = new ArrayList<>();
                            for (String imageStr : item.getImage()) {
                                selectList.add(new LocalMedia(imageStr, 1000, PictureMimeType.ofImage(), null));
                            }
                            showLarImage(selectList, 0);
                        }
                    }
                });
            } else if (item.getImage().size() > 1) {
                layout_one_pic.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);

                PingjiaImagesAdapter pingjiaImagesAdapter = new PingjiaImagesAdapter(R.layout.item_pingjia_images);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
                gridLayoutManager.setSmoothScrollbarEnabled(true);
                recyclerView.setLayoutManager(gridLayoutManager);
                pingjiaImagesAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                        if (mGlideSimpleLoader != null) {
//                            mGlideSimpleLoader.showUrl(item.getImage(), position);
//                        }
                        if (item.getImage() != null && item.getImage().size() > 0) {
                            List<LocalMedia> selectList = new ArrayList<>();
                            for (String imageStr : item.getImage()) {
                                selectList.add(new LocalMedia(imageStr, 1000, PictureMimeType.ofImage(), null));
                            }
                            showLarImage(selectList, position);
                        }


                    }
                });
                recyclerView.setAdapter(pingjiaImagesAdapter);
                recyclerView.setNestedScrollingEnabled(false);
                recyclerView.setFocusable(false);
                pingjiaImagesAdapter.setNewData(item.getImage());

            }
        }


    }

    public void showLarImage(List<LocalMedia> selectList, int position) {
        PictureSelector.create((Activity) mContext)
                .themeStyle(R.style.picture_default_style) // xml设置主题
//                        .setPictureStyle(mPictureParameterStyle)// 动态自定义相册主题
                //.setPictureWindowAnimationStyle(animationStyle)// 自定义页面启动动画
                .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)// 设置相册Activity方向，不设置默认使用系统
                .isNotPreviewDownload(true)// 预览图片长按是否可以下载
                //.bindCustomPlayVideoCallback(callback)// 自定义播放回调控制，用户可以使用自己的视频播放界面
                .loadImageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                .openExternalPreview(position, selectList);
    }
}
